package com.xnx3.wangmarket.plugin.innerAccount.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.StringUtil;
import com.xnx3.j2ee.Global;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.func.ActionLogCache;
import com.xnx3.j2ee.func.Captcha;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.service.UserService;
import com.xnx3.j2ee.shiro.ShiroFunc;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.admin.bean.UserBean;
import com.xnx3.wangmarket.admin.controller.BaseController;
import com.xnx3.wangmarket.plugin.base.controller.BasePluginController;

/**
 * 用户登陆
 * @author 管雷鸣
 */
@Controller
@RequestMapping("/plugin/innerAccount/")
public class UserLoginInnerAccountPluginController extends BasePluginController {

	@Resource
	private SqlService sqlService;
	@Resource
	private UserService userService;

	/**
	 * 登陆页面
	 */
	@RequestMapping("login${url.suffix}")
	public String login(HttpServletRequest request, Model model){
		String link = Global.get("PLUGIN_INNERACCOUNT_LOGINSUCCESS_LINK");
		if(link == null || link.length() == 0){
			return error(model,"请先登陆总管理后台-插件管理-内部账户，设置登陆成功后跳转到的网址");
		}
		
		if(haveUser()){
			//已登陆了，那么直接跳转
			return redirect(link);
		}
		return "plugin/innerAccount/login";
	}
	

	/**
	 * 登陆请求验证
	 * @param request {@link HttpServletRequest} 
	 * 		<br/>登陆时form表单需提交三个参数：username(用户名/邮箱)、password(密码)、code（图片验证码的字符）
	 * @return vo.result:
	 * 			<ul>
	 * 				<li>0:失败</li>
	 * 				<li>1:成功，成功后，info会返回跳转的链接地址，如： plugin/bbs/view.do?id=123</li>
	 * 			</ul>
	 */
	@RequestMapping("loginSubmit${url.suffix}")
	@ResponseBody
	public BaseVO loginSubmit(HttpServletRequest request,Model model){
		String link = Global.get("PLUGIN_INNERACCOUNT_LOGINSUCCESS_LINK");
		if(link == null || link.length() == 0){
			return error("请先登陆总管理后台-插件管理-内部账户，设置登陆成功后跳转到的网址");
		}
		
		//验证码校验
		BaseVO capVO = Captcha.compare(request.getParameter("code"), request);
		if(capVO.getResult() == BaseVO.FAILURE){
			ActionLogCache.insert(request, "innerAccount用户名密码模式登录失败", "验证码出错，提交的验证码："+StringUtil.filterXss(request.getParameter("code")));
			return capVO;
		}
	
		//验证码校验通过
		BaseVO baseVO =  userService.loginByUsernameAndPassword(request);
		if(baseVO.getResult() == BaseVO.SUCCESS){
			//登录成功,BaseVO.info字段将赋予成功后跳转的地址，所以这里要再进行判断
			
			//用于缓存入Session，用户的一些基本信息，比如用户的站点信息、用户的上级代理信息、如果当前用户是代理，还包含当前用户的代理信息等
			UserBean userBean = new UserBean();
			//找到site，以便ue编辑器上传图片，上传到指定站点目录下
			ShiroFunc.getCurrentActiveUser().setUeUploadParam1("innerAccountPlugin");
//			userBean.setSite(sqlService.findById(Site.class, siteid));
			
			//得到当前登录的用户的信息
//			User user = getUser();
			
			//将用户相关信息加入Shiro缓存
			ShiroFunc.getCurrentActiveUser().setObj(userBean);
			
			ActionLogCache.insert(request, "登陆论坛成功","登陆用户:"+request.getParameter("username"));
			
			//登陆成功后跳转到的链接
			return success(link);
		}else{
			ActionLogCache.insert(request, "innerAccount用户名密码模式登录失败",baseVO.getInfo());
		}
		
		return baseVO;
	}
}